package com.isoftstone.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.isoftstone.pojo.Users;
import com.isoftstone.service.UsersService;

@Controller
/* @RequestMapping("/users") */
public class UsersController {

		@Autowired
		private UsersService usersService;

		//添加,跳转到adduser.jsp
		@RequestMapping(path = "/adduser",method = RequestMethod.GET)
		public String toadduser() {
			System.out.println("添加");
			return "adduser";
		}
		//添加,实现按钮action
		@RequestMapping(path = "/addusers",method = RequestMethod.POST)
		public String adduser(Model model,Users users) {
		if (users.getUsername() != null) {
			java.util.Date dt = new java.util.Date();
			java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = sdf.format(dt);
			users.setCreatetime(currentTime);
			System.out.println(currentTime);
			int i = usersService.register(users);
			if(i > 0)
				return "adduser";
		   }
			return "adduser";
		}
		
		//查询,listuser.jsp
		@RequestMapping("/user")
		public String list(HttpServletRequest request, HttpServletResponse response) {
			List<Users> users = usersService.getAllUsers();			
			request.setAttribute("users", users);
			return "listuser";
		}

		//修改,跳转到updateuser.jsp
		@RequestMapping("/selectuser")
		public String toupdate(Model model, String id) {
			Users users = usersService.getUsersById(Integer.parseInt(id));
			model.addAttribute("users", users);
			return "updateuser";
		}
		//修改,实现按钮action
		@RequestMapping("/updateuser")
		public String updateusers(Model model,String id,Users user) {
			java.util.Date dt = new java.util.Date();
			java.text.SimpleDateFormat sdf =new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String currentTime = sdf.format(dt);
			user.setCreatetime(currentTime);
			usersService.update(user);
			Users users = usersService.getUsersById(Integer.parseInt(id));
			model.addAttribute("users", users);
			return "updateuser";
		}
		
		//删除,并跳转到listuser.jsp
		@RequestMapping("/deleteuser")
		public String deleteusers(Model model,String id) {
			int j=Integer.parseInt(id);
			System.out.println(j);
			int i = usersService.dismiss(Integer.parseInt(id));
			List<Users> users = usersService.getAllUsers();
			model.addAttribute("users", users);
			return "listuser";
		}
		
		//登录验证
		@RequestMapping(path = "/login",method = RequestMethod.POST)
		public String login(Model model,String username,String password,HttpSession session) {
			if(username!=null) {
				int i = usersService.check(username, password);
				if (i>0) {
					Users user = usersService.getUsersByName(username);
					session.setAttribute("user",user);
					return "manage";
				}
			}			
			return "error";
		}
}
